package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.PanghuCommunity;
import com.hualan.dao.CommunityListDao;

import com.hualan.dao.impl.CommunityListDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/communityList/update")
public class CommunityListAddServlet extends HttpServlet {

    private CommunityListDao communityListDao = new CommunityListDaoImpl();
    //--POST
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int communityCount = 0;
        int row = 0;

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Map<String, Object> map = new HashMap<>();

        if(req.getParameter("communityId") != null && !"".equals(req.getParameter("communityId"))){
            communityCount = 1;
        }

       if(communityCount == 0){
           // 1、获取请求参数
           String communityAddress = req.getParameter("communityAddress");
           String communityAreaCode = req.getParameter("communityAreaCode");
           String communityCity = req.getParameter("communityCity");
           String communityCoordinates = req.getParameter("communityCoordinates");
           String communityDistrict = req.getParameter("communityDistrict");
           String communityLatitude = req.getParameter("communityLatitude");
           String communityLongitude = req.getParameter("communityLongitude");
           String communityName = req.getParameter("communityName");
           String communityProvince = req.getParameter("communityProvince");

           //新增  封装成实体对象
            row = communityListDao.addCommunity(new PanghuCommunity(null, communityName, communityAddress,
                   null, communityProvince, communityCity, communityDistrict, 0, communityLatitude,
                   communityLongitude, null, "", 0, 0, 0));

       }else{
           // 1、获取请求参数
           int communityId = Integer.parseInt(req.getParameter("communityId"));
           String communityAddress = req.getParameter("communityAddress");
           String communityAreaCode = req.getParameter("communityAreaCode");
           String communityCity = req.getParameter("communityCity");
           String communityCoordinates = req.getParameter("communityCoordinates");
           String communityDistrict = req.getParameter("communityDistrict");
           String communityLatitude = req.getParameter("communityLatitude");
           String communityLongitude = req.getParameter("communityLongitude");
           String communityName = req.getParameter("communityName");
           String communityProvince = req.getParameter("communityProvince");
// 把数据封装成实体对象
            row = communityListDao.updateCommunity(new PanghuCommunity(communityId, communityName, communityAddress, null, communityProvince, communityCity, communityDistrict, 0, communityLatitude, communityLongitude, null, "", 0, 0, 0));
       }
        if (row < 1) {
            map.put("code", 302);
            map.put("message", "新增失败!");
        } else {
            map.put("code", 200);
            map.put("message", "新增成功!");
        }
        resp.getWriter().write(JSON.toJSONString(map));

    }

//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
//        doPost(req, resp);
//
//    }

}
